﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace AffinityChooser
{
    public partial class Form1 : Form
    {
       private string baseDrive = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System));
       private string baseSystem = Environment.SystemDirectory;
       private string fileLoc = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
       private OpenFileDialog open = new OpenFileDialog();
       private string name;
        
       
        public Form1()
        {
            InitializeComponent();
            
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            
            
            open.Title = "Browse for Application";
            open.Filter = "Executable Files(*.exe)|*.exe|All Files(*.*)|*.*";
            open.InitialDirectory = baseDrive;

            if (open.ShowDialog() == DialogResult.OK)
            {
                name = Path.GetFileNameWithoutExtension (open.FileName);
                txtPath.Text = open.FileName;
                btnAdd.Enabled = true;
                
               
            }

        }



        private void AddHandler(object sender, EventArgs e)
        {
            AddToList(name);
            dataGridView1.DataSource = ReadList();

        }

        private void AddToList(string name)
        {
           
            using (StreamWriter fileWrite = new StreamWriter(fileLoc + "\\applist.txt", true))
            {
                fileWrite.WriteLine(open.FileName);
                fileWrite.WriteLine(name);


            }

            
        }


        private List<AppData> ReadList()
        {
            List<AppData> appList = new List<AppData>();
            using (StreamReader readFile = new StreamReader(fileLoc + "\\applist.txt"))
            {
                while (readFile.Peek() > -1)
                {
                    appList.Add( new AppData {
                        Path = readFile.ReadLine(),
                        Name = readFile.ReadLine()
                    });
            

                }
                return appList;

            }

        }

        private struct AppData
        {
            public string Name { get; set; }
            public string Path { get; set; }
        }

        private void Form_Load(object sender, EventArgs e)
        {
            btnAdd.Enabled = false;
            try
            {
                dataGridView1.DataSource = ReadList();
            }
            catch
            {
                //Do nothing
            }
           

        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string path = dataGridView1.Rows[e.RowIndex].Cells["Path"].Value.ToString();
            string name2 = dataGridView1.Rows[e.RowIndex].Cells["Name"].Value.ToString();
            string execute = "cmd.exe";
            string args = " /c start "+ "\"" + name2 + "\"" + " /affinity 1 " + "\"" +path + "\"";
            Process.Start(execute, args);
        }


    }
}
